package com.jinlia.show.modules.wxapp.controller;

import com.jinlia.show.common.core.utils.UserInfoUtils;
import com.jinlia.show.modules.base.constant.EnableStatusEnum;
import com.jinlia.show.modules.base.model.po.GoodsLabelPO;
import com.jinlia.show.modules.base.model.query.GoodsLabelQuery;
import com.jinlia.show.modules.base.wxapp.entity.AppConfig;
import com.jinlia.show.modules.base.wxapp.entity.ShoppingCart;
import com.jinlia.show.modules.base.wxapp.entity.SwiperImgs;
import com.jinlia.show.modules.base.wxapp.service.AppConfigMapperService;
import com.jinlia.show.modules.base.wxapp.service.ShoppingCartMapperService;
import com.jinlia.show.modules.base.wxapp.service.SwiperImgsMapperService;
import com.jinlia.show.modules.wxapp.service.HomeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private SwiperImgsMapperService swiperImgsMapperService;
    @Autowired
    private HomeService homeService;
    @Autowired
    private ShoppingCartMapperService shoppingCartMapperService;
    @Autowired
    private AppConfigMapperService appConfigMapperService;

    /**
     * 轮播图
     *
     * @return
     */
    @GetMapping(value = "/swiperImgs")
    public ResponseEntity<List<SwiperImgs>> getSwiperImgs() {
        List<SwiperImgs> list = swiperImgsMapperService.list();
        return ResponseEntity.ok(list);
    }

    /**
     * 首页标签
     *
     * @return
     */
    @PostMapping(value = "/goods/tag")
    public ResponseEntity<List<GoodsLabelPO>> getGoodsTag(@RequestBody GoodsLabelQuery goodsTagQuery) {
        List<GoodsLabelPO> list = homeService.getGoodsLabel(goodsTagQuery);
        return ResponseEntity.ok(list);
    }

    /**
     * 购物车数量
     *
     * @return
     */
    @GetMapping(value = "/shopping/card/badge")
    public ResponseEntity<Long> updateShoppingCardTabBarBadge() {
        Long count = shoppingCartMapperService.lambdaQuery()
                .eq(ShoppingCart::getEnableStatus, EnableStatusEnum.ENABLE.getValue())
                .eq(ShoppingCart::getUserId, UserInfoUtils.getUserId())
                .count();
        return ResponseEntity.ok(count);
    }

    @GetMapping(value = "/config/{id}")
    private AppConfig getConfig(@PathVariable("id") Long id) {
        AppConfig byId = appConfigMapperService.getById(id);
        return byId;
    }
}
